package cn.fntop.interceptor;


import cn.fntop.context.TenantContextHolder;
import cn.fntop.constant.TenantConstants;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 租户信息拦截,传递 RestTemplate 请求头的租户ID
 */
public class TenantRequestInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        Integer tenantId = TenantContextHolder.getTenantId();
        if (tenantId != null) {
            request.getHeaders().set(TenantConstants.TENANT_ID, tenantId.toString());
        }
        return execution.execute(request, body);
    }
}
